package com.hodgepodge.commons.validator.constraint;

import com.hodgepodge.commons.validator.annotation.FixedStrValues;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * title FixedValuesValidator
 * projectName colorful-blog
 * desc 在固定字符值范围内校验
 *
 * @author jiangjian
 * @date 2021/6/2 17:15
 */
public class FixedStrValuesValidator implements ConstraintValidator<FixedStrValues, String> {

    private String[] values;

    @Override
    public void initialize(FixedStrValues fixedStrValues) {
        this.values = fixedStrValues.values();
    }

    @Override
    public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) {
        return Arrays.asList(values).contains(str);
    }
}
